//
//  Brands_BrandsMainBrandList_Tbv_WX.m
//  ZITHER
//
//  Created by weixin on 2019/4/8.
//  Copyright © 2019 weixn. All rights reserved.
//

#import "Brands_BrandsMainBrandList_Tbv_WX.h"
#import "MainSeachBrandCell.h"
#import "MainSeachBCell.h"
#import "UITableView+SCIndexView.h"
#import "Brands_BrandsMainBrandList_Cell_WX.h"

@implementation Brands_BrandsMainBrandList_Tbv_WX

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = HexColor(0xf5f5f5);
        [self def];
        [self loadDate];
    }
    return self;
}

-(void)def{
    self.dataSource = self;
    self.delegate = self;
    
    SCIndexViewConfiguration *configuration = [SCIndexViewConfiguration configurationWithIndexViewStyle:0];
    [self setConfiguration:configuration];
    self.sc_indexViewConfiguration = configuration;
    self.sc_translucentForTableViewInNavigationBar = YES;
    self.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.rowHeight = UITableViewAutomaticDimension;

}

-(void)loadDate{
    
    [[serviceDefualt loadGoodsSearchConditions:[searchConditionsModel class]]subscribeNext:^(ResponseData * x) {
        self.model = x.responseData;
        
        NSDictionary *dic =[self.model.brandInfo yy_modelToJSONObject];
        
        self.dicModel = [[NSMutableDictionary alloc]initWithDictionary:dic];
        
        NSMutableArray *indexViewDataSource = [NSMutableArray array];
        
        NSArray *newArray = [[self.dicModel allKeys] sortedArrayUsingSelector:@selector(compare:)];
        
        
        for (NSString *str in newArray) {
            //填充字母
            [indexViewDataSource addObject:str];
            [self.arrM addObject: self.dicModel[str]];
            [self.arrMBigWord addObject:str];
        }
        
        @try{
            //代码写在这个里面
            self.sc_indexViewDataSource = indexViewDataSource.copy;
        }
        @catch(NSException *exception) {
            NSLog(@"异常错误是:%@", exception);
        }
        @finally {
            
        }
        [self reloadData];
        
    }];
    
}

-(void)setConfiguration:(SCIndexViewConfiguration*)configuration{
    
    configuration.indicatorBackgroundColor = baseColorTheme;
    configuration.indicatorHeight = 50;
    configuration.indicatorRightMargin = 50;
    configuration.indicatorCornerRadius = 20;
    configuration.indexItemBackgroundColor = [UIColor clearColor];
    configuration.indexItemSelectedBackgroundColor = [UIColor whiteColor];
    configuration.indexItemHeight = 14;
    configuration.indexItemRightMargin = 2;
    configuration.indexItemsSpace = 2;
    
    configuration.indexItemTextColor = [UIColor colorWithHex:@"333333"];
    configuration.indicatorTextColor = [UIColor whiteColor];//选中文字颜色
    configuration.indexItemSelectedTextColor = baseColorTheme;//右边选中文字颜色
    configuration.indicatorHeight = 60;
    
    //    @property (nonatomic, assign) CGFloat indexItemHeight;                      // 索引元素高度
    
}
-(NSMutableDictionary *)dicModel{
    if (!_dicModel) {
        _dicModel = [[NSMutableDictionary alloc]init];
    }
    return _dicModel;
}

-(NSMutableArray *)arrM{
    if (!_arrM) {
        _arrM = [[NSMutableArray alloc]init];
    }
    return _arrM;
}

-(NSMutableArray *)arrMBigWord{
    if (!_arrMBigWord) {
        _arrMBigWord = [[NSMutableArray alloc]init];
    }
    return _arrMBigWord;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//    NSLog(@"indexPath == %@",indexPath);
//    NSLog(@"");
//    if (self.block) {
//        self.block(self.arrM[indexPath.section][indexPath.row]);
//        [self.navigationController popViewControllerAnimated:YES];
//    }
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSLog(@"count == %ld",self.arrM.count);
    return self.arrM.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *arr = self.arrM[section];
    return arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return  [self getBrands_BrandsMainBrandList_Cell_WX:indexPath];
}

-(Brands_BrandsMainBrandList_Cell_WX*)getBrands_BrandsMainBrandList_Cell_WX:(NSIndexPath *)indexPath{
    
    static NSString *sectionCell = @"Brands_BrandsMainBrandList_Cell_WX";
    Brands_BrandsMainBrandList_Cell_WX *cell = [self dequeueReusableCellWithIdentifier:sectionCell];
    if (cell == nil) {
        cell = [[Brands_BrandsMainBrandList_Cell_WX alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionCell];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    NSString *brandName = self.arrM[indexPath.section][indexPath.row][@"brandNameNative"];
    
    cell.title.text = brandName;
    
    cell.titleSub.text =self.arrM[indexPath.section][indexPath.row][@"brandName"];
    
    return cell;
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 30)];
    view.backgroundColor = HexColor(0xf5f5f5);
    BaseLab *lab = [BaseLab initCustomLab:self.arrMBigWord[section] color:HexColor(0x333333) font:[UIFont fontWithName:@"PingFangSC-Medium" size:17]];
    lab.frame = CGRectMake(15, 0, 20, 30);
    lab.backgroundColor = HexColor(0xf5f5f5);
    [view addSubview:lab];
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}


//设置区头不悬浮
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat sectionHeaderHeight = 50;
    
    if(scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
        
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0,0);
        
    } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
        
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
        
    }
    
}



@end
